package com.qingmu.club.minaapi.config;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据模型生成工具
 * @Author: duke
 * @Date: 2019-08-07 11:44
 */
public class MysqlGenerator {


    public static String[] table_lists=new String[]{
            "activity",
            "activity_center",
            "admin_menu",
            "admin_module",
            "admin_user",
            "admin_user_group",
            "award",
            "banner",
            "club",
            "club_shop",
            "club_staff",
            "club_staff_exam",
            "club_staff_remark",
            "club_staff_score",
            "club_staff_shop",
            "club_users",
            "club_users_classhours",
            "club_users_course",
            "club_users_integral",
            "club_users_remark",
            "club_users_shop",
            "cmoneylog",
            "course",
            "course_classhour",
            "evaluation",
            "exam_item",
            "exam_item_detail",
            "grow_college",
            "grow_college_category",
            "order_info",
            "integral_goods",
            "integral_goods_category",
            "learn_diary",
            "learn_diary_reply",
            "magic_group",
            "magic_group_rule",
            "push_message",
            "users",
            "users_wx",
            "club_staff_exam",
            "club_staff_score",
            "order_info",
            "magic_group_joinlist",
            "magic_group_rule",
            "magic_group_run",
            "users_share_relation",
            "integral_share_present",
            "integral_attention_present"
    };

    /**
     * <p>
     * MySQL 生成演示
     * </p>
     */
    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        String OS = System.getProperty("os.name").toLowerCase();
        if (OS.indexOf("mac os") != -1) {
            projectPath = projectPath + "/club-mina-api";
        }
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("duke");
        gc.setOpen(true);
        gc.setIdType(IdType.ID_WORKER_STR);//AUTO自增, NONE未设置, INPUT用户输入ID, ID_WORKER全局唯一ID, UUID, ID_WORKER_STR;，ID_Worker是雪花算法生成的主键
        // service 命名方式
        gc.setServiceName("%sService");
        // service impl 命名方式
        gc.setServiceImplName("%sServiceImpl");
        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);

        gc.setControllerName("%sPlusController");

        // XML 二级缓存
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columList
        gc.setBaseColumnList(false);
        mpg.setGlobalConfig(gc);

        // TODO 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://120.24.70.202:3306/weskater?verifyServerCertificate=false&useSSL=false&requireSSL=false&useUnicode=true&characterEncoding=UTF-8&characterSetResults=utf8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("caipiao");
        dsc.setPassword("Aisi@2017");
        mpg.setDataSource(dsc);

        // TODO 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("plus");
        pc.setParent("com.qingmu.club.minaapi");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        mpg.setPackageInfo(pc);

        // 自定义需要填充的字段
        List<TableFill> tableFillList = new ArrayList<>();

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        dsc.setTypeConvert(new MySqlTypeConvert() {
            @Override
            public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                if (fieldType.toLowerCase().contains("datetime")) {
                    return DbColumnType.DATE;
                }
                return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
            }
        });
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null).setController(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        // 设置逻辑删除键
        strategy.setLogicDeleteFieldName("deleted");
        // TODO 指定生成的bean的数据库表名
        strategy.setInclude(MysqlGenerator.table_lists);
        //strategy.setSuperEntityColumns("id");
        strategy.setEntityColumnConstant(true);
        // 驼峰转连字符
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        mpg.execute();
    }

}
